package com.tumblshow;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbAdapter {
	/*	GLOBAL CONSTANTS	*/
	private static final String DATABASE_NAME = "TumblShow.db";
	private static final int DATABASE_VERSION = 1;

	private static final String TUMBLR_TABLE = "tumblr";
	private static final String IMG_TABLE = "img";

	public static final String TUMBLR_ID = "_id";
	public static final String TUMBLR_TITLE = "title";
	public static final String TUMBLR_LINK = "link";

	public static final String IMG_ID = "_id";
	public static final String IMG_TUMBLR = "tumblr";
	public static final String IMG_LINK = "link";
	
	private static final String TUMBLR_CREATE = "create table " + TUMBLR_TABLE + " (" + TUMBLR_ID + " integer primary key autoincrement, " + 
		TUMBLR_TITLE + " text not null, " + TUMBLR_LINK + " text not null);";
	private static final String IMG_CREATE = "create table " + IMG_TABLE + " (" + IMG_ID + " integer primary key autoincrement, " + 
		IMG_TUMBLR + " integer, " + IMG_LINK + " text not null);";

	/*	MEMBER VARIABLES	*/
	private SQLiteDatabase db;
	public static Context ctx;
	private DbHelper dbHelper;
	private boolean dbOpen = false;
	
	/*	CONSTRUCTORS	*/
	public DbAdapter(Context ctx) {
		DbAdapter.ctx = ctx;
		dbHelper = new DbHelper(ctx, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/*	CUSTOM METHODS	*/
	public DbAdapter open() throws SQLException {
		db = dbHelper.getWritableDatabase();
		dbOpen = true;
		return this;
	}
	
	public void close() {
		db.close();
		dbOpen = false;
	}
	
	public boolean isOpen(){
		return dbOpen;
	}

	//	CREATE METHODS
	public long tumblrCreate(String title, String link){
		ContentValues initialValues = new ContentValues();
		initialValues.put(TUMBLR_TITLE, title);
		initialValues.put(TUMBLR_LINK, link);
		
		return db.insert(TUMBLR_TABLE, null, initialValues);
	}

	public long imgCreate(long id, String link){
		ContentValues initialValues = new ContentValues();
		initialValues.put(IMG_TUMBLR, id);
		initialValues.put(IMG_LINK, link);
		
		return db.insert(IMG_TABLE, null, initialValues);
	}

	//	DELETE METHODS
	public boolean tumblrDeleteAll(){
		return db.delete(TUMBLR_TABLE, TUMBLR_ID + ">0" , null) > 0;
	}

	public boolean tumblrDeleteId(long id){
		return db.delete(TUMBLR_TABLE, TUMBLR_ID + "=" + id , null) > 0;
	}

	public boolean imgDeleteAll(long id){
		return db.delete(IMG_TABLE, IMG_ID + ">0" , null) > 0;
	}

	//	FETCH ALL METHODS
	public Cursor tumblrFetchAll(){
		Cursor cursor = db.query(TUMBLR_TABLE, new String[] { TUMBLR_ID, TUMBLR_TITLE, TUMBLR_LINK }, null, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
		return cursor;
	}

	public Cursor imgFetchAll(){
		Cursor cursor = db.query(IMG_TABLE, new String[] { IMG_ID, IMG_TUMBLR, IMG_LINK }, null, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
		return cursor;
	}
	
	//	FETCH BY METHODS
	public Cursor imgFetchTumblr(long id) throws SQLException {
		Cursor cursor = db.query(IMG_TABLE, new String[] { IMG_ID, IMG_TUMBLR, IMG_LINK }, IMG_TUMBLR + "=" + id, 
			null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
		return cursor;
	}

	/*	NESTED CLASSES	*/
	private static class DbHelper extends SQLiteOpenHelper {
		public DbHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(TUMBLR_CREATE);
			_db.execSQL(IMG_CREATE);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
			_db.execSQL("DROP TABLE IF EXISTS " + TUMBLR_TABLE);
			_db.execSQL("DROP TABLE IF EXISTS " + IMG_TABLE);
			
			onCreate(_db);
		}
	}
}
